package com.angel.kuaidi.json;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class CXJsonNode {

    private JSONObject m_pJsonDoc;
    private JSONArray m_JSONArray;

    public CXJsonNode(JSONObject m_pJsonDoc) {
        this.m_pJsonDoc = m_pJsonDoc;
    }

    public CXJsonNode(JSONArray m_JSONArray) {
        this.m_JSONArray = m_JSONArray;
    }

    // 获得节点的值
    public String getValue(String name) throws JSONException {
        if (m_pJsonDoc.isNull(name))
            return "";
        return m_pJsonDoc.getString(name);
    }

    public int getNumber(String name) throws JSONException {
        if (m_pJsonDoc.isNull(name))
            return 0;
        return m_pJsonDoc.getInt(name);
    }

    public double getDouble(String name) throws JSONException {
        if (m_pJsonDoc.isNull(name))
            return 0;
        return m_pJsonDoc.getDouble(name);
    }

    public boolean getBool(String name) throws JSONException {
        if (m_pJsonDoc.isNull(name))
            return false;
        return m_pJsonDoc.getBoolean(name);
    }

    public CXJsonNode getArray(String name) throws JSONException {
        if (m_pJsonDoc.isNull(name))
            return null;
        return new CXJsonNode(m_pJsonDoc.getJSONArray(name));
    }

    // 获得子节点指针
    public CXJsonNode getSubNode(String subname) throws JSONException {
        if (m_pJsonDoc.isNull(subname))
            return null;
        JSONObject jSONObject = m_pJsonDoc.getJSONObject(subname);
        return new CXJsonNode(jSONObject);
    }

    // 获取子节点的长度
    public int getSubNodeLength() {
        return m_pJsonDoc.length();
    }

    // 判断此节点是否存在
    public boolean has(String value) {
        return m_pJsonDoc.has(value);
    }

    // 获得数组的值
    public String getValue(int index) throws JSONException {
        return m_JSONArray.getString(index);
    }

    public int getNumber(int index) throws JSONException {
        return m_JSONArray.getInt(index);
    }

    public double getDouble(int index) throws JSONException {
        return m_JSONArray.getDouble(index);
    }

    public boolean getBool(int index) throws JSONException {
        return m_JSONArray.getBoolean(index);
    }

    // 获得数组子节点指针
    public CXJsonNode getSubNode(int index) throws JSONException {
        return new CXJsonNode(m_JSONArray.getJSONObject(index));
    }

    // 获得数组的子数组指针
    public CXJsonNode getSubArray(int index) throws JSONException {
        return new CXJsonNode(m_JSONArray.getJSONArray(index));
    }

    // 获取数组的长度
    public int getArrayLength() {
        return m_JSONArray.length();
    }

    // 获取json节点的字符串
    public String toNodeString() {
        return m_pJsonDoc.toString();
    }

    // 获取json数组的字符串
    public String toArrayString() {
        return m_JSONArray.toString();
    }

    // 添加子节点
    public CXJsonNode addNewNode(String aNode) throws JSONException {
        JSONObject objectNode = new JSONObject();
        m_pJsonDoc.put(aNode, objectNode);
        return new CXJsonNode(objectNode);
    }

    public CXJsonNode addNewArray(String aArray) throws JSONException {
        JSONArray array = new JSONArray();
        m_pJsonDoc.put(aArray, array);
        return new CXJsonNode(array);
    }

    // 添加与设置节点
    public void setValue(String aNode, String aValue) throws JSONException {
        m_pJsonDoc.put(aNode, aValue);
    }

    public void SetNumber(String aNode, int aNum) throws JSONException {
        m_pJsonDoc.put(aNode, aNum);
    }

    public void SetDouble(String aNode, double aDouble) throws JSONException {
        m_pJsonDoc.put(aNode, aDouble);
    }

    public void SetBool(String aNode, boolean aBool) throws JSONException {
        m_pJsonDoc.put(aNode, aBool);
    }
}
